<template>
	<view>
		<u-navbar class="custom-header" back-text="返回" title="其他入库列表" height="44" :custom-back="backToIndex">
			<view class="u-nav-slot" slot="left"><u-icon name="search" size="19"></u-icon></view>
			<view slot="right">
				<u-icon
					@click="navTo('/pages/sys/home/components/work_bench/other-in/other_in_inquiry')"
					name="search"
					color="#0055ff"
					size="38"
					style="margin-right: 30rpx;"
				></u-icon>
				<u-button type="primary" size="mini" style="margin-right: 20rpx;" @click="navTo('/pages/sys/home/components/work_bench/other-in/other_in')">新增</u-button>
			</view>
		</u-navbar>
		<uni-list>
			<uni-list-item v-for="(workReport, index) in workList" :key="index">
				<template slot="body">
					<uni-card is-full class="uni-list-card" padding="0" spacing="0">
						<view class="list-item-content">
							<view class="list-item-body">
								<view class="item-label">入库单号：</view>
								<view class="u-line-1 item-value">{{ workReport.recptCode }}</view>
							</view>
							<view class="list-item-body">
								<view class="item-label">入库单名称：</view>
								<view class="u-line-1 item-value">{{ workReport.recptName }}</view>
							</view>
							<view class="list-item-body">
								<view class="item-label">供应商：</view>
								<view class="u-line-1 item-value">{{ workReport.vendorName }}</view>
							</view>
							<view class="list-item-body">
								<view class="item-label">采购订单号：</view>
								<view class="u-line-1 item-value">{{ workReport.poCode }}</view>
							</view>
							<view class="list-item-body">
								<view class="item-label">检验单号：</view>
								<view class="u-line-1 item-value">{{ workReport.iqcCode }}</view>
							</view>
							<view class="list-item-body">
								<view class="item-label">入库日期：</view>
								<view class="u-line-1 item-value">{{ workReport.recptDate }}</view>
							</view>
							<view class="flex-botton" v-if="workReport.status != 'FINISHED'">
								<u-button class="btn" type="primary" size="mini" @click="handleEnter(workReport)">执行入库</u-button>
								<u-button class="btn btn1" type="primary" size="mini" @click="handleUpdate(workReport)">修改</u-button>
								<u-button class="btn" type="primary" size="mini" @click="handleDelete()">删除</u-button>
								<u-modal v-model="show" @confirm="confirm(workReport)" :content="content" show-cancel-button></u-modal>
							</view>
						</view>
					</uni-card>
				</template>
			</uni-list-item>
		</uni-list>
		<u-loadmore :status="status" :icon-type="iconType" :load-text="loadText" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			status: 'loadmore',
			iconType: 'flower',
			loadText: {
				loadmore: '上拉加载更多',
				loading: '努力加载中',
				nomore: '已加载全部内容'
			},
			show: false,
			content: '是否确认删除该入库单？',
			params: {
				pageNum: 1,
				pageSize: 10 //默认每次请求10条
			},
			workList: [], //列表
			total: '' //总数据量
		};
	},
	onLoad(options) {
		console.log(options);
		if (JSON.stringify(options) != '{}') {
			const parmas = JSON.parse(options.params);
			this.params = parmas;
			this.getList();
		} else {
			this.resetParmas();
			this.getList();
		}
	},
	onReachBottom() {
		console.log('到达底部了');
		//判断是否下一页还有数据
		if (this.params.pageNum * this.params.pageSize >= this.total) {
			this.status = 'nomore';
			uni.showToast({
				title: '已全部加载完成',
				duration: 1000,
				icon: 'none'
			});
		} else {
			// 让页码值自增 +1
			this.params.pageNum += 1;
			//再次获取列表数据
			this.getList();
			this.status = 'loadmore';
		}
	},
	methods: {
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		//获取列表
		getList() {
			this.workList = [];
			this.$u.api.otherIn
				.getOtherInList(this.params)
				.then(res => {
					let newArr = [];
					newArr = [...this.workList, ...res.rows];
					let map = new Map();
					for (let item of newArr) {
						map.set(item.recptCode, item);
					}
					this.workList = [...map.values()];
					this.total = res.total;
					if (res.total == 0) {
						uni.showToast({
							title: '暂无数据',
							duration: 2000
						});
					}
					if (this.total <= 10) {
						this.status = 'nomore';
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		//执行入库
		handleEnter(row) {
			const recptId = row.recptId;
			this.$u.api.otherIn.handleOtherInExecute({ recptId: recptId }).then(res => {
				if (res.code === 200)
					uni.showToast({
						title: '入库成功',
						duration: 1000,
						icon: 'none'
					});
				this.getList();
			});
		},
		//修改
		handleUpdate(row) {
			const recptId = row.recptId;
			uni.navigateTo({
				url: '/pages/sys/home/components/work_bench/other-in/other_in?params=' + recptId
			});
		},
		//删除
		handleDelete() {
			this.show = true;
		},
		//提示窗确认是否删除
		confirm(val) {
			this.$u.api.otherIn
				.handleOtherInDelete({ recptIds: val.recptId })
				.then(res => {
					if (res.code === 200)
						uni.showToast({
							title: '删除成功',
							duration: 1000,
							icon: 'none'
						});
					this.getList();
				})
				.catch(err => {
					console.log(err);
				});
		},
		//重置参数
		resetParmas() {
			this.params ==
				{
					pageNum: 1,
					pageSize: 10 //默认每次请求10条
				};
		},
		//返回主页
		backToIndex() {
			uni.reLaunch({
				url: '/pages/sys/home/index'
			});
		}
	}
};
</script>

<style scoped lang="scss">
.uni-list-card {
}

.item-card-title-layout {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	margin: 0 20rpx;
	padding: 20rpx 0;
	border-bottom: 1px solid rgb(214, 215, 217);

	.item-card-title {
		font-size: 32rpx;
		color: #5a5a5a;
	}

	.item-card-status {
		padding: 5rpx 8rpx;
		font-size: 26rpx;
		color: #fff;

		&.status-1 {
			background-color: #9a9998;
		}

		&.status-2 {
			background-color: #f9ae3d;
		}

		&.status-3 {
			background-color: #3c9cff;
		}

		&.status-4 {
			background-color: #5ac725;
		}
		&.status-5 {
			background-color: #f56c6c;
		}
	}
}

.list-item-content {
	padding: 20rpx;
	border-bottom: 1px solid rgb(214, 215, 217);

	.list-item-body {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		line-height: 56rpx;

		.item-label,
		.item-value {
			color: #6a6a6a;
			font-size: 28rpx;
		}
	}
}
.card-actions {
	display: flex;
	flex-direction: row;
	align-items: center;
	.card-actions-item {
		font-size: 25rpx;
		flex: 1;
		text-align: center;
		padding: 20rpx 0;
		&.withdraw {
			color: #f56c6c;
		}
		&.division {
			border-right: 1px solid rgb(214, 215, 217);
		}
	}
}
.flex-botton {
	display: flex;
	justify-content: flex-end;
}
.btn {
	margin: 0;
}
.btn1 {
	margin-right: 20rpx;
	margin-left: 20rpx;
}
</style>
